<script setup>
import { ref, onUnmounted  } from 'vue'
import { userLoginService  } from '@/api/user.js'
import { showToast } from 'vant'
import { useUserStore } from '@/stores/modules/user.js'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const totalSecond = ref(60); // 总秒数
const second = ref(60); // 倒计时的秒数
const timer = ref(null); // 定时器 id
const loginParam = ref({
      phonenumber: '19558559960',
      imageCode: '',
      code: ''
    })
    const captchaUrl = ref(null) // 验证码图片的 url
    const validFnPhone = () => {
      if (!/^1[3-9]\d{9}$/.test(loginParam.value.phonenumber)) {
        showToast('请输入正确的手机号')
        return false
      }
      return true
    }
    const validFn = () => {
      if (!/^1[3-9]\d{9}$/.test(loginParam.value.phonenumber)) {
        showToast('请输入正确的手机号')
        return false
      }
      if (!/^\w{5}$/.test(loginParam.value.imageCode)) {
        showToast('请输入正确的图形验证码')
        return false
      }
      return true
    }
    
const refreshCaptcha = () => {
  if (!validFnPhone()) {
    return
  }
  console.log('refreshCaptcha');
  captchaUrl.value = `http://localhost:8080/app/users/image/${loginParam.value.phonenumber}?t=${Date.now()}`;
}
const  getCode = async  () => {
  if (!validFn()) {
    return
  }
  if (!timer.value && second.value === totalSecond.value) {
    // 开启倒计时
    timer.value = setInterval(() => {
      second.value--

      if (second.value < 1) {
        clearInterval(timer.value)
        timer.value = null
        second.value = totalSecond.value
      }
    }, 1000)

    // 发送请求，获取验证码
    showToast('发送成功，请注意查收')
  }
}

onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value);
    timer.value = null;
  }
  console.log('Component is unmounted');
})
const setToken = useUserStore().setToken
const getUser = useUserStore().getUser
const router = useRouter()

const login = async() => {
  
  // if (!validFn()) {
  //   return
  // }
  
  const data = await userLoginService(loginParam.value)
  console.log("登录后返回的信息" + data.data.token);
  console.log("token" + data.data.token);
  setToken(data.data.token)
  await getUser()
  const url = route.query.backUrl || '/home'
  router.replace(url)
  // router.push('/home')
}
</script>

<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="loginParam.phonenumber"  class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="loginParam.imageCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="captchaUrl !== null" :src="captchaUrl" alt="" @click="refreshCaptcha" style="width: 94px; height: 31px; ">
          <img v-if="captchaUrl === null" src="@/assets/image.png" alt="" @click="refreshCaptcha" style="width: 94px; height: 31px; ">
        </div>
        
 
        <div class="form-item">
          <input v-model="loginParam.code" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode">
              {{ second === totalSecond ? '获取验证码' : second + `秒后重新发送`}}
          </button>

        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>


<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>